package com.learn.demo.provider;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.learn.demo.entities.kafka.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * 消息发送类
 *
 * @author banjiawei
 * @date 2022/03/16
 */
@Component
public class KafkaProvider {
    @Autowired
    private KafkaTemplate kafkaTemplate;

    /**
     * 发送消息
     * @author banjiawei
     * @date 2022/3/16
     * @param message
     * @return void
     */
    public void send(Message message) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            kafkaTemplate.send("user", objectMapper.writeValueAsString(message));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
